package com.export.service;

import com.alibaba.fastjson.JSON;
import com.export.dao.CustomerReportDao;
import com.export.dao.OperateLogDao;
import com.export.entity.CustomerReport;
import com.export.entity.CustomerReportDetail;
import com.export.entity.DownloadLog;
import com.export.entity.ReportParam;
import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户报表
 *
 * @author yuanqinglong
 * @since 2021/7/22 9:12
 */
@Service
public class CustomerReportServiceImpl implements IReportService<ReportParam> {


    @Autowired
    private OperateLogDao operateLogDao;

    @Autowired
    private CustomerReportDao customerReportDao;


    /**
     * 导出表
     *
     * @param reportParam 报告参数
     */
//    @Async
    @Override
    public void exportReportTable(ReportParam reportParam) {
        TableNode tableNode = customerReportDao.customerReportExportList(reportParam);
        List<CustomerReport> customerReports = JSON.parseArray(tableNode.toJson(), CustomerReport.class);
        customerReports.forEach(customerReport -> customerReport.processingAttributeValue(reportParam));
        this.saveLogCreateExcelUpload(customerReports, CustomerReport.class, "客户报表", reportParam.getOperatorId());
    }

    /**
     * 导出详情列表
     *
     * @param reportParam 报告参数
     */
//    @Async
    @Override
    public void exportDetailTable(ReportParam reportParam) {
        TableNode detailList = customerReportDao.customerReportDetailExportList(reportParam);
        List<CustomerReportDetail> customerReportDetailList = JSON.parseArray(detailList.toJson(), CustomerReportDetail.class);
        customerReportDetailList.forEach(CustomerReportDetail::calculateRate);
        this.saveLogCreateExcelUpload(customerReportDetailList, CustomerReportDetail.class, "客户报表详情", reportParam.getOperatorId());
    }



    /**
     * 下载日志
     *
     * @param reportParam 报告参数
     * @param cycleIndex  循环次数
     * @return {@link List<DownloadLog>}
     */
    @Override
    public List<DownloadLog> downloadLog(ReportParam reportParam, int cycleIndex) {
        int offset = (cycleIndex - 1) * LIMIT_SIZES;
        TableNode accountLog = operateLogDao.customerLog(reportParam.getStartDate(), reportParam.getEndDate(), reportParam.getCustId(), offset, LIMIT_SIZES);
        return JSON.parseArray(accountLog.toJson(), DownloadLog.class);
    }


    /**
     * 下载日志数
     *
     * @param reportParam 报告参数
     * @return int
     */
    @Override
    public int downloadLogCount(ReportParam reportParam) {
        return operateLogDao.customerLogCount(reportParam.getStartDate(), reportParam.getEndDate(), reportParam.getCustId()).valueInt();
    }





}
